<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page import="java.util.*, book.BookBean" %>
<%
	// リクエスト文字列のエンコードを設定
	request.setCharacterEncoding("UTF-8");
	// パラメータの取得
	Integer crtPage = (Integer)request.getAttribute("page");
	Integer cnt = (Integer)request.getAttribute("cnt");
	String msg = (String)request.getAttribute("msg");
	// 検索パラメータの取得
	//	String title = request.getParameter("title");
	//	String writer = request.getParameter("writer");
	//	String publisher = request.getParameter("publisher");
	//	String genre = request.getParameter("genre");
	//	String isbn = request.getParameter("isbn");
	//	String keyword = request.getParameter("keyword");
	@SuppressWarnings("unchecked")
	Map<String, String[]> m = (Map<String, String[]>)session.getAttribute("BookSerchParameter");
	String[] titles = (String[])m.get("title");
	String[] writers = (String[])m.get("writer");
	String[] publisherIds = (String[])m.get("publisher");
	String[] genreIds = (String[])m.get("genre");
	String[] isbns = (String[])m.get("isbn");
	String[] keywords = (String[])m.get("keyword");
	String[] urls = (String[])m.get("url");

	String title = titles[0];
	String writer = writers[0];
	String publisher = publisherIds[0];
	String genre = genreIds[0];
	String isbn = isbns[0];
	String keyword = keywords[0];

	// ページングURLの取得
	String pageUrl = request.getQueryString();
	if (pageUrl != null) {
		int index = pageUrl.indexOf("&page=");
		if (index < 0) {
			pageUrl = urls[0];
		}
		else {
			pageUrl = pageUrl.substring(0,index);
		}
	}
	else {
		pageUrl = urls[0];
	}
	// ページ表示件数を取得
	int max = BookBean.PAGE_MAX;
%>
<c:import url="/share/layout.jsp">
    <c:param name="title" value="書籍検索結果"/>
    <c:param name="css" value="booksearch.css"/>
    <c:param name="content">
        <jsp:attribute name="value">
<!-- contents start -->
<% if(msg != null && !"".equals(msg)) { %>
<div id="msg_area">
	<%= msg %>
</div>
<% } %>
<div id="search">
	<% if(cnt == 0) { %>
		<p id="result_red">以下の条件に一致する書籍はありません</p>
	<% } else { %>
		<p id="result">以下の条件に一致した書籍は <strong><%= cnt %></strong> 冊あります</p>
	<% } %>
	<div>
		<% BookBean bb = new BookBean(); %>
		タイトル：<% if("".equals(title)) { %>
			<span class="val">---</span>
		<% } else { %>
			<span class="val"><%= title %></span>
		<% } %>
		　著作者：<% if("".equals(writer)) { %>
			<span class="val">---</span>
		<% } else { %>
			<span class="val"><%= writer %></span>
		<% } %>
		　出版者：<% if("".equals(publisher)) { %>
			<span class="val">---</span>
		<% } else {
			bb.setPublisherId(Integer.parseInt(publisher)); %>
			<span class="val"><%= bb.getPublisher() %></span>
		<% } %>
		　ジャンル：<% if("".equals(genre)) { %>
			<span class="val">---</span>
		<% } else {
			bb.setGenreId(Integer.parseInt(genre)); %>
			<span class="val"><%= bb.getGenre() %></span>
		<% } %>
		　ISBN：<% if("".equals(isbn)) { %>
			<span class="val">---</span>
		<% } else { %>
			<span class="val"><%= isbn %></span>
		<% } %>
		<br />内容キーワード：<% if("".equals(keyword)) { %>
			<span class="val">---</span>
		<% } else { %>
			<span class="val"><%= keyword %></span>
		<% } %>
	</div>
</div>

<form id="mng_form" action="MenuServlet">
	<table class="list_table">
		<tr>
			<th id="id" colspan="2">ID</th>
			<th id="title">タイトル</th>
			<th id="writer">著作者</th>
			<th id="publisher">出版者</th>
			<th id="rental">貸出中</th>
		</tr>
		<%
		@SuppressWarnings("unchecked")
		List<BookBean> list = (List<BookBean>)request.getAttribute("list");
		for(int i=0; i<list.size(); i++) {
			BookBean book = list.get(i);
		%>
		<tr <% if(book.isDeleteFlag()) { %>class="delete"<% } else if(i%2!=0) { %>class="color"<% } %>>
			<td align="center"><input type="radio" name="id" value="<%= book.getId() %>" /></td>
			<td align="center"><%= book.getId() %></td>
			<td><%= book.getTitle() %></td>
			<td><%= book.getWriter() %></td>
			<td><%= book.getPublisher() %></td>
			<td style="text-align:center;"><% if(book.getRentalId() > 0) { %>●<% } %></td>
		</tr>
		<%
		}
		%>
	</table>

	<div id="paging_area">
		<%
			double dMax = cnt / (double)max;
			int maxPage = (int)Math.ceil(dMax);
		%>
		<%= crtPage %> / <%= maxPage %><br />
		<%
			if(maxPage > 1) {
				String linkPage = "/Books/book/SearchServlet?" + pageUrl + "&page=";
				if(crtPage > 1) {
					int p = crtPage - 1;
					out.println("<a href=\"" + linkPage + p + "\">前へ</a>");
				}
				for(int i=0, p=1; i<cnt; i+=max, p++) {
					out.println("<a href=\"" + linkPage + p + "\">" + p + "</a>");
				}
				if(crtPage * max < cnt) {
					int p = crtPage + 1;
					out.println("<a href=\"" + linkPage + p + "\">次へ</a>");
				}
			}
		%>
	</div>

	<input type="hidden" name="url" value="<%= request.getQueryString() %>" />

	<input class="button" type="submit" name="back" value="戻る" />
	<input class="button" type="submit" name="rental" value="貸出" />
	<input class="button" type="submit" name="delete" value="削除" />
	<input class="button" type="submit" name="edit" value="変更" />
	<input class="button" type="submit" name="entry" value="新規登録" />
	<input class="button" type="submit" name="detail" value="詳細" />

</form>
<!-- contents end -->
        </jsp:attribute>
    </c:param>
</c:import>